// var i;
// var $item_Bag_CiTiao_UIBindings = require("./Item_Bag_CiTiao_UIBindings");
// var $resKeeper = require("./ResKeeper");
// var $excel = require("./Excel");
// var $playerDataManager = require("./PlayerDataManager");
// var $bundleConfig = require("./BundleConfig");
// var $commonUtils = require("./CommonUtils");
// var $bagCiTiaoRichText = require("./BagCiTiaoRichText");
// var $unLockLight = require("./UnLockLight");
// var $hallManager = require("./HallManager");
// var m = cc._decorator;
// var v = m.ccclass;
// var b = m.property;
// var w = m.inspector;
// var I = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.auto_ui = null;
//         t.ciTiaoComp = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.StartSet = function (e) {
//         var t = this;
//         var o = $playerDataManager.PlayerDataMgr.GetEquipDataById(e.shuiguoId);
//         this.openData = e;
//         var n = this.openData.citiaomiaoshu;
//         $excel.Excel.shuiguo(e.shuiguoId);
//         this.ciTiaoComp.shuiGuoId = e.shuiguoId;
//         this.ciTiaoComp.ciTiaoId = e.id;
//         n = $commonUtils.default.replaceSymbols(n, "n", [""]);
//         var i = $excel.Excel.shuiguobuff(this.openData.citiaoshuzhitype);
//         var a;
//         if (i) {
//             a = i.value;
//         } else {
//             a = null;
//         }
//         var r = [n];
//         if (-1 != n.indexOf("X")) {
//             r = n.split("X");
//         }
//         var c = "";
//         for (var s = 0; s < r.length; s++) {
//             var l = r[s];
//             if (-1 != l.indexOf("#")) {
//                 var u = "<img src='player" + (e.shuiguoId - 1e3) + "_5' click='ShowZaZhongInfo' />";
//                 l = $commonUtils.default.replaceSymbols(l, "#", [u]);
//             }
//             c += "<color=#ffffff>" + l + "</c>";
//             if (s < r.length - 1 && a) {
//                 c += "<color=#71ff00>" + a[s] + "</c>";
//             }
//         }
//         if (o.level < this.openData.jiesuo || !o.isUnLock) {
//             c = "<color=#a3f3ff><size=26>LV" + this.openData.jiesuo + "解锁:</s></c><color=#333333>" + c + "</c>";
//         } else {
//             if (!$hallManager.HallMgr.GetCiTiaoIsLock(e.id)) {
//                 $hallManager.HallMgr.SetCiTiaoIsLock(e.id);
//                 if (0 != this.node.getSiblingIndex()) {
//                     this._loadRes($bundleConfig.BundleNames.Common, "prefab/UnLockLight", cc.Prefab).then(function (e) {
//                         var o = cc.instantiate(e);
//                         o.parent = t.node;
//                         var n = t.auto_ui.node_bgFather.children[0];
//                         var i = cc.size(n.width, n.height);
//                         var a = n.getComponent(cc.Sprite).spriteFrame;
//                         o.getComponent($unLockLight.default).StartSet(i, a, 1);
//                     });
//                 }
//             }
//         }
//         c = "<outline color=#000000 width=3>" + (c = "<b>" + c + "</b>") + "</outline>";
//         this.auto_ui.node_bgFather.children[0].active = o.level >= this.openData.jiesuo && o.isUnLock;
//         this.auto_ui.node_bgFather.children[1].active = o.level < this.openData.jiesuo || !o.isUnLock;
//         this.auto_ui.rtxt_infoLabel.string = c;
//         this.LoadSprite(e.id);
//     };
//     t.prototype.LoadSprite = function (e) {
//         return __awaiter(this, void 0, void 0, function () {
//             var t = this;
//             return __generator(this, function () {
//                 this._loadRes($bundleConfig.BundleNames.Common, "res/skillicon/skill_" + e, cc.SpriteFrame).then(
//                     function (e) {
//                         t.auto_ui.sp_iconSprite.spriteFrame = e;
//                         $commonUtils.default.LimitSpriteMaxSize(t.auto_ui.sp_iconSprite, 70);
//                     }
//                 );
//                 return [2];
//             });
//         });
//     };
//     __decorate([b($item_Bag_CiTiao_UIBindings.default)], t.prototype, "auto_ui", void 0);
//     __decorate([b($bagCiTiaoRichText.default)], t.prototype, "ciTiaoComp", void 0);
//     return __decorate([v], t);
// })($resKeeper.default);
// exports.default = I;

import ItemBagCiTiaoUIBindings from './Item_Bag_CiTiao_UIBindings';
import ResKeeper from './ResKeeper';
import { Excel } from './Excel';
import { PlayerDataMgr } from './PlayerDataManager';
import { BundleNames } from './BundleConfig';
import CommonUtils from './CommonUtils';
import BagCiTiaoRichText from './BagCiTiaoRichText';
import LightMaskController from './UnLockLight';
import { HallMgr } from './HallManager';

const { ccclass, property, inspector } = cc._decorator;

/**
 * 建议类名：ItemBagCiTiao
 */
@ccclass
class ItemBagCiTiao extends ResKeeper {
    @property(ItemBagCiTiaoUIBindings)
    public auto_ui: ItemBagCiTiaoUIBindings = null;

    @property(BagCiTiaoRichText)
    public ciTiaoComp: BagCiTiaoRichText = null;

    public openData: any;

    /**
     * 初始化设置
     * @param params - 传入的参数对象
     * 建议方法名：initializeSettings
     */
    public initializeSettings(params: any): void {
        const equipData = PlayerDataMgr.getEquipDataById(params.shuiguoId);
        this.openData = params;
        let description = this.openData.citiaomiaoshu;
        Excel.shuiguo(params.shuiguoId);
        this.ciTiaoComp.shuiGuoId = params.shuiguoId;
        this.ciTiaoComp.ciTiaoId = params.id;
        description = CommonUtils.replaceStringSymbols(description, "n", [""]);
        const buffData = Excel.shuiguobuff(this.openData.citiaoshuzhitype);
        let buffValue: string | null = buffData ? buffData.value : null;
        let descriptionParts = [description];
        if (description.indexOf("X") !== -1) {
            descriptionParts = description.split("X");
        }
        let formattedDescription = "";
        for (let i = 0; i < descriptionParts.length; i++) {
            let part = descriptionParts[i];
            if (part.indexOf("#") !== -1) {
                const imgTag = `<img src='player${params.shuiguoId - 1000}_5' click='ShowZaZhongInfo' />`;
                part = CommonUtils.replaceStringSymbols(part, "#", [imgTag]);
            }
            formattedDescription += `<color=#ffffff>${part}</c>`;
            if (i < descriptionParts.length - 1 && buffValue) {
                formattedDescription += `<color=#71ff00>${buffValue[i]}</c>`;
            }
        }
        if (equipData.level < this.openData.jiesuo || !equipData.isUnLock) {
            formattedDescription = `<color=#a3f3ff><size=26>LV${this.openData.jiesuo}解锁:</s></c><color=#333333>${formattedDescription}</c>`;
        } else {
            if (!HallMgr.isCiTiaoUnlocked(params.id)) {
                HallMgr.unlockCiTiao(params.id);
                if (this.node.getSiblingIndex() !== 0) {
                    this._loadRes(BundleNames.Common, "prefab/UnLockLight", cc.Prefab).then((prefab) => {
                        const unlockLightNode = cc.instantiate(prefab);
                        unlockLightNode.parent = this.node;
                        const bgNode = this.auto_ui.node_bgFather.children[0];
                        const size = cc.size(bgNode.width, bgNode.height);
                        const spriteFrame = bgNode.getComponent(cc.Sprite).spriteFrame;
                        unlockLightNode.getComponent(LightMaskController).initialize(size, spriteFrame, 1);
                    });
                }
            }
        }
        formattedDescription = `<outline color=#000000 width=3><b>${formattedDescription}</b></outline>`;
        this.auto_ui.node_bgFather.children[0].active = equipData.level >= this.openData.jiesuo && equipData.isUnLock;
        this.auto_ui.node_bgFather.children[1].active = equipData.level < this.openData.jiesuo || !equipData.isUnLock;
        this.auto_ui.rtxt_infoLabel.string = formattedDescription;
        this.loadSprite(params.id);
    }

    /**
     * 加载精灵
     * @param id - 精灵的ID
     * 建议方法名：loadSprite
     */
    public async loadSprite(id: number): Promise<void> {
        const spriteFrame = await this._loadRes(BundleNames.Common, `res/skillicon/skill_${id}`, cc.SpriteFrame);
        this.auto_ui.sp_iconSprite.spriteFrame = spriteFrame;
        CommonUtils.limitSpriteSize(this.auto_ui.sp_iconSprite, 70);
    }
}

export default ItemBagCiTiao;